/*************************************************************************
    > File Name: mid_average_filter.c
    > Author: zengxuemin
    > Mail: suse110@foxmail.com 
    > Created Time: 2016年10月12日 星期三 17:16:15
 ************************************************************************/

#include <stdio.h>
#include <string.h>
/**
*@brief 中位值平均滤波算法
*@param 
*   buffer [in] 
*   count  [in] 最大不超过64
*@retval 滤波结果       
*/


unsigned short xvals[]= {2863,2863,2788,2788,2851,2851,2971,2971,2637,2637,2898,2898,2793,2793,2858,2858,2847,2847,2867,2867,2843,2843,2849,2849,2843,2843,2835,2835,2828,2828,2838,2838,2824,2824,2818,2818,2819,2819,2815,2815,2809,2809,2804,2804,2790,2790,2788,2788,2779,2779,2767,2767,2753,2753,2743,2743,2739,2739,2733,2733,2729,2729,2721,2721,2713,2713,2710,2710,2707,2707};                                                                              
unsigned short yvals[]={1603,1603,1882,1882,1949,1949,2062,2062,2074,2074,2228,2228,2185,2185,2147,2147,1984,1984,2462,2462,2492,2492,2573,2573,2662,2662,2724,2724,2768,2768,2697,2697,2902,2902,2961,2961,3025,3025,3108,3108,3166,3166,3230,3230,3278,3278,3337,3337,3390,3390,3436,3436,3504,3504,3545,3545,3580,3580,3612,3612,3638,3638,3660,3660,3679,3679,3692,3692,3702,3702};
unsigned short zvals[]={669,659,653,702,687,770,719,695,636,764,686,681,748,677,685,666,647,666,727,721,770,765,657,630,702,710,691,648,705,677,756,672,730,688,714,709,673,615,620,621,600,605,606,604,714,605,655,645,605,604,601,620,604,611,582,608,667,649,598,500,520,643,605,553,564,558,565,559,576,525,};

void Bublesort(unsigned short *a,int n)
{
     int i,j,m;
     unsigned short k;
     for(j=0;j<n;j++)   /* 气泡法要排序n次*/
     {
          for(i=0;i<n-j-1;i++)  /* 值比较大的元素沉下去后，只把剩下的元素中的最大值再沉下去就可以啦 */
          {
               if(a[i]>a[i+1])  /* 把值比较大的元素沉到底 */
               {
                    k=a[i];
                    a[i]=a[i+1];
                    a[i+1]=k;
               }
//printf("sorted temp:%d,%d\n",i,j);
for (m=0; m<n; m++) {
//	printf("%d ", a[m]);
}

//printf("\n");               
          }
     }
}
unsigned short mid_ave_filter(unsigned short *buffer, unsigned char count)
{ 

  unsigned int i,j,k,l; 

  unsigned short temp,value; 
  unsigned int sum = 0;
  unsigned short value_buf[64]={0};

  memcpy(value_buf, buffer, count*sizeof(buffer)); 

printf("no sorted values1\n");
for (i=0; i<count; i++) {
	printf("%d ", buffer[i]);
}
printf("\n");

  //采样值从小到大排列（冒泡法）
	Bublesort(value_buf, count);
printf("sorted values\n");
for (i=0; i<count; i++) {
	printf("%d ", value_buf[i]);
}
printf("\n");
  for(i=1;i<count-1;i++) 
    sum += value_buf[i];
  value = sum/(count-2); 
printf("ave:%d\n",value);
  return value; 
}

  
int main(int argc, char*argv[])
{
    unsigned short newxval[100]={0},val;
    int i=0,j;
    for (i=0,j=0; i<sizeof(xvals)/sizeof(unsigned short); i+=5,j++) {
        newxval[j] = mid_ave_filter(&xvals[i], 5);
    }
    for(i=0; i<sizeof(xvals)/sizeof(unsigned short)/5; i++)
		printf("%d ", newxval[i]);
	printf("\n");
    return 0;
}












